/**
 * 项目名：五位一体化工安全信息平台
 * 日期：    2019-11-25 13:02:05
 * Copyright (c) 2015- joysuch-版权所有
 */

package com.joysuch.wwyt.bp.controller;

import com.joysuch.wwyt.bp.bean.BpDeviceFacilityInspectRecordBean;
import com.joysuch.wwyt.bp.bean.MhBindPointBean;
import com.joysuch.wwyt.bp.bean.facility.BpProductionAddBean;
import com.joysuch.wwyt.bp.bean.facility.BpProductionFacilityListBean;
import com.joysuch.wwyt.bp.bean.facility.BpProductionFacilityScrapBean;
import com.joysuch.wwyt.bp.bean.facility.BpProductionSearchBean;
import com.joysuch.wwyt.bp.entity.BpProductionFacility;
import com.joysuch.wwyt.bp.service.BpProductionFacilityService;
import com.joysuch.wwyt.core.common.ajax.ResultBean;
import com.joysuch.wwyt.core.common.util.PageDataUtils;
import io.swagger.annotations.Api;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/bp/production")
@Api(tags = "设备设施管理-生产装置")
public class BpProductionFacilityController {

    private static final Logger logger = LoggerFactory.getLogger(BpProductionFacilityController.class);

    @Autowired
    private BpProductionFacilityService bpProductionFacilityService;

    @PostMapping("add")
    public Object add(@RequestBody @Valid BpProductionAddBean entity) {
        BpProductionFacility facility = bpProductionFacilityService.add(entity);
        return ResultBean.success(facility);
    }

    @PostMapping("update")
    public Object update(@RequestBody @Valid BpProductionAddBean entity) {
        BpProductionFacility facility = bpProductionFacilityService.update(entity);
        return ResultBean.success(facility);
    }

    @GetMapping("page")
    public ResultBean page(Pageable page, BpProductionSearchBean bpProductionSearchBean) {
        page = PageDataUtils.addOrderByDesc(page, "id");
        Page<BpProductionFacilityListBean> list = bpProductionFacilityService.findByPage(page, bpProductionSearchBean);
        return ResultBean.pageData(list.getContent(), list.getTotalElements());
    }

    @GetMapping("inspect-record-page")
    public ResultBean inspectRecordPage(Pageable page, Long deviceFacilityId) {
        // page = PageDataUtils.addOrderByDesc(page, "id");
        com.baomidou.mybatisplus.extension.plugins.pagination.Page<BpDeviceFacilityInspectRecordBean> result =
                bpProductionFacilityService.findInspectRecordPage(page,
                deviceFacilityId);
        return ResultBean.pageData(result.getRecords(),result.getTotal());

    }

    @GetMapping("/getById")
    public ResultBean getById(Long id) {
        BpProductionFacilityListBean byId = bpProductionFacilityService.findById(id);
        return ResultBean.wrapSuccessfulResult(byId);
    }

    @PostMapping("delete")
    public ResultBean delete(Long id) {
        bpProductionFacilityService.delete(id);
        return ResultBean.success("");
    }

    @PostMapping("deleteByIds")
//    @RequiresPermissions("bp:production:delete")
    public ResultBean deleteByIds(@RequestBody List<Long> ids) {
        bpProductionFacilityService.deleteByIds(ids);
        return ResultBean.success("");
    }

    @RequestMapping("simple-list")
    public Object simpleList(Long id) {
        try {
            return ResultBean.success("");
        } catch (Exception e) {
            logger.error("删除失败", e);
            return ResultBean.fail(101, "删除失败");
        }
    }

    @RequestMapping("scrap")
    public Object scrap(@RequestBody BpProductionFacilityScrapBean bean) {
        try {
            bpProductionFacilityService.scrapFacility(bean);
            return ResultBean.success("");
        } catch (Exception e) {
            logger.error("失败", e);
            return ResultBean.fail(101, "删除失败");
        }
    }

    @RequestMapping("get-scrap-info")
    public Object getScrapInfo(Long id) {
        try {
            BpProductionFacilityScrapBean info = bpProductionFacilityService.getScrapInfo(id);
            return ResultBean.success(info);
        } catch (Exception e) {
            logger.error("删除失败", e);
            return ResultBean.fail(101, "删除失败");
        }
    }

    @PostMapping("bind-monitor-point")
    public Object bindMonitorPoint(@RequestBody MhBindPointBean bean) {
        String errorMessage = bpProductionFacilityService.bindMonitorPoint(bean, 0,null);
        if (StringUtils.isNotBlank(errorMessage)) {
            throw new IllegalArgumentException(errorMessage);
        }
        return ResultBean.success("");
    }

    @GetMapping("unbind-monitor-point")
    public Object unbindMonitorPoint(Long id, Long monitorPointId) {
        try {
            bpProductionFacilityService.unbindMonitorPoint(id, monitorPointId);
            return ResultBean.success("");
        } catch (Exception e) {
            logger.error("删除失败", e);
            return ResultBean.fail(101, "删除失败");
        }
    }

}
